package com.yanti.controller;

import com.yanti.domain.ResponseResult;
import com.yanti.domain.vo.MessageVO;
import com.yanti.repository.ChatHistoryRepository;
import com.yanti.util.MongoChatMemory;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.messages.Message;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.function.Function;

@RestController
@RequiredArgsConstructor
@RequestMapping("/ai/history")
public class ChatHistoryController {

    private final ChatHistoryRepository chatHistoryRepository;

    private final MongoChatMemory mongoChatMemory;

    @GetMapping("/getChatIds/{type}")
    public ResponseResult<?> getChatIds(@PathVariable String type) {

        return ResponseResult.okResult(chatHistoryRepository.getChatIds(type));
    }

    @GetMapping("/{type}/{chatId}")
    public ResponseResult<?> getChatHistory(@PathVariable("type") String type, @PathVariable("chatId") String chatId) {
        List<Message> messages = mongoChatMemory.get(chatId, Integer.MAX_VALUE);
        if(messages == null){
            return ResponseResult.okResult(List.of());
        }
        return ResponseResult.okResult(messages.stream().map(MessageVO::new).toList());
    }
}
